unit Class_ZipUtil;

interface
uses
  Classes,Windows,SysUtils,FileCtrl,VCLZip,VCLUnZip;
type
  TZipUtil=class(TObject)
  public
    class function GetAllSubDir(Directory: string; var RetList: TStringList): Boolean;
    class function GetAllDirFile(Directory: string; var RetList: TStringList): Boolean;
    class function ZipDir(sDir, sFile: string): Boolean;
    class function ZipFil(sFil, tFile: string): Boolean;
    class function UnZipDir(sFile, sDir: string): Boolean;
  end;

implementation

{ TZipUtil }

class function TZipUtil.GetAllDirFile(Directory: string;
  var RetList: TStringList): Boolean;
var
  i: Integer;
  DirList: TStringList;
  SearchRec: TSearchRec;
begin
  Result := False;
  DirList := TStringList.Create;
  DirList.Add(Directory + '\');
  if not GetAllSubDir(Directory + '\', DirList) then exit;

  for i := 0 to DirList.Count - 1 do
  begin
    if FindFirst(DirList.Strings[i] + '*.*', faAnyFile, SearchRec) = 0 then
    begin
      repeat
        if SearchRec.Attr <> faDirectory then
          RetList.Add(DirList.Strings[i] + SearchRec.Name);
      until (FindNext(SearchRec) <> 0);
    end;
  end;
  if DirList.Count <= 0 then
  begin
    RetList.Add(Directory);
  end;
  DirList.Free;
  Result := True;
end;

class function TZipUtil.GetAllSubDir(Directory: string;
  var RetList: TStringList): Boolean;
var  
  SearchRec: TSearchRec;
  sTemp: string;
  function IsSubDir(SearchRec: TSearchRec): Boolean;
  begin
    if (SearchRec.Attr = faDirectory) and (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
      Result := True
    else
      Result := False;
  end;
begin
  if FindFirst(Directory + '*.*', faAnyFile, SearchRec) = 0 then
  begin
    repeat 
      if IsSubDir(SearchRec) then
      begin
        sTemp := Directory + SearchRec.Name + '\';
        RetList.Add(sTemp);
        GetAllSubDir(sTemp, RetList); 
      end;
    until (FindNext(SearchRec) <> 0);
  end;
  FindClose(SearchRec);
  Result := True;
end;

class function TZipUtil.UnZipDir(sFile, sDir: string): Boolean;
var
  VCLUnZip1: TVCLUnZip;
begin
  Result := True;
  VCLUnZip1 := TVCLUnZip.Create(nil);
  with VCLUnZip1 do
  begin
    ZipName := sFile;
    ReadZip;
    Destdir := sDir;
    RecreateDirs := True;
    FilesList.Add('*.*');
    DoAll := True;
    OverwriteMode := Always;
  end;
  // Screen.Cursor := crHourglass;
  try
    VCLUnZip1.UnZip;
  except
    Result := False;
  end;
  //Screen.Cursor := crDefault;
  VCLUnZip1.Free;
end;

class function TZipUtil.ZipDir(sDir, sFile: string): Boolean;
var
  VCLZip1: TVCLZip;
  RetList: TStringList;
begin
  Result := True;
  VCLZip1 := TVCLZip.Create(nil);
  RetList := TStringList.Create;
  GetAllDirFile(sDir, RetList);
  with VCLZip1 do
  begin
    FilesList := RetList;
    ZipName := sFile;
    RelativePaths := True;
  end;
  VCLZIP1.RootDir := SDIR;

  //Screen.Cursor := crBusy;
  try
    VCLZip1.Zip;
  except
    Result := False;
  end;
  //Screen.Cursor := crDefault;
  RetList.Free;
  VCLZip1.Free;
end;

class function TZipUtil.ZipFil(sFil, tFile: string): Boolean;
var
  VCLZip1: TVCLZip;
  RetList: TStringList;
begin
  Result := True;
  VCLZip1 := TVCLZip.Create(nil);
  RetList := TStringList.Create;
  RetList.Add(sFil);
  with VCLZip1 do
  begin
    FilesList := RetList;
    ZipName := tFile;
    RelativePaths := True;
  end;
  VCLZIP1.RootDir := ExtractFileDir(sFil);

  //Screen.Cursor := crBusy;
  try
    VCLZip1.Zip;
  except
    Result := False;
  end;
  //Screen.Cursor := crDefault;
  RetList.Free;
  VCLZip1.Free;
end;

end.
